using System;
using System.Web.Mvc;
using Cooking.Core.Dto;
using Cooking.Core.Model;
using Cooking.Core.Services;
using Cooking.Web.Code;
using Cooking.Web.ViewData;
using Cooking.Web.Views.Shared;

namespace Cooking.Web.Controllers
{
    public class RecipeController : AppController
    {
        private readonly IRecipeService _recipeService;

        public RecipeController(IRecipeService recipeService)
        {
            _recipeService = recipeService;
        }

        public string StatusMessage
        {
            get
            {
                return TempData["StatusMessage"] as string ?? string.Empty;
            }

            set
            {
                TempData["StatusMessage"] = value;
            }

        }

        public ActionResult Index()
        {
            return View("Create");
        }
        
        public ActionResult Create(string description)
        {
            var result = _recipeService.CreateNewRecipe(description);

            TempData["RecipeResult"] = result;
            return RedirectToAction("Edit", new {result.RecipeId});
        }

        public ActionResult Edit(int recipeId)
        {
            Recipe recipe = _recipeService.LoadRecipe(recipeId);

            EditRecipeViewData viewData = new EditRecipeViewData{ Recipe = recipe};
            
            return View("Edit", viewData);
        }


        public ActionResult AddIngredient([Bind(Prefix  = "addDto")]AddToRecipeDto dto)
        {
            _recipeService.AddIngredientToRecipe(dto);
            StatusMessage = "Added ingredient to recipe.";
            return RedirectToAction("Edit", new {dto.RecipeId});
        }

        
        /// <summary>
        /// This works, but how do you make it degrade for when js isnt available?
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public ActionResult AddIngredientAjax([Bind(Prefix = "addDto")]AddToRecipeDto dto)
        {
            _recipeService.AddIngredientToRecipe(dto);
            return PartialView("IngredientList", new EditRecipeViewData {Recipe = _recipeService.LoadRecipe(dto.RecipeId)});
        }


        public ActionResult EditIngredient(int recipeId, int ingredientId)
        {
            var recipe = _recipeService.LoadRecipe(recipeId);
            EditRecipeViewData viewData = new EditRecipeViewData {Recipe = recipe, EditIngredientId = ingredientId};
            return View("Edit", viewData);
        }

        public ActionResult UpdateIngredient(UpdateRecipeIngredientDto dto)
        {
            _recipeService.ChangeIngredient(dto);
            StatusMessage = "Updated ingredient";
            return RedirectToAction("Edit", new {dto.RecipeId});

        }

        public ActionResult DeleteIngredient(int recipeId, int recipeIngredientId)
        {
            _recipeService.RemoveIngredient(recipeId, recipeIngredientId);
            StatusMessage = "Removed ingredient";
            return RedirectToAction("Edit", new {recipeId});
        }

        public ActionResult AddStep(int recipeid, string description)
        {
            _recipeService.AddStepToRecipe(recipeid, description);
            StatusMessage = "Added step.";
            return RedirectToAction("Edit", new {recipeid});
        }

        public ActionResult EditStep(int recipeId, int stepId)
        {
            var recipe = _recipeService.LoadRecipe(recipeId);

            EditRecipeViewData vd = new EditRecipeViewData
                                        {
                                            Recipe = recipe,
                                            EditStepId = stepId
                                        };

            return View("Edit", vd);
        }

        public ActionResult UpdateStep(int recipeId, int stepId, string description)
        {
            _recipeService.UpdateStep(recipeId, stepId, description);
            StatusMessage = "Updated Step";
            return RedirectToAction("Edit", new {recipeId});
        }

        public ActionResult DeleteStep(int recipeId, int stepId)
        {
            _recipeService.RemoveStep(recipeId, stepId);
            StatusMessage = "Removed step";
            return RedirectToAction("Edit", new {recipeId});

        }

        public ActionResult MoveStepUp(int recipeId, int stepId)
        {
            _recipeService.MoveStepUp(recipeId, stepId);
            StatusMessage = "Moved step up";
            return RedirectToAction("Edit", new {recipeId});
        }

        public ActionResult MoveStepDown(int recipeId, int stepId)
        {
            _recipeService.MoveStepDown(recipeId, stepId);
            StatusMessage = "Moved step down";
            return RedirectToAction("Edit", new { recipeId });
        }



    }
}